/* @(#)GSAfficheAddAction.java    V0.1
 *
 * Project: NEWGS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-5-24
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.upload.FormFile;

import system.LoginContext;

import com.hitrust.gs.form.GSAfficheForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.GenRandNum;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.bean.GSAfficheBean;
import com.hitrust.gs.bean.GSBargainBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSAfficheProcessor;

/**
 * 
 */
public class GSAfficheAddAction extends GSBaseAction{

    public String execute(GSBaseForm aForm) throws Exception {
        GSAfficheForm tForm=(GSAfficheForm)aForm;
        LoginContext context = (LoginContext) aForm.getSession().getAttribute(
                GSBaseConstants.LOGIN);
//        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        
        SimpleDateFormat dftime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String buftime = dftime.format(new java.util.Date(System.currentTimeMillis()));
        
        GSAfficheBean tGSAfficheBean=new GSAfficheBean();
        BeanUtils.copyProperties(tGSAfficheBean,tForm);
        tGSAfficheBean.setGs_affiche_id(GenRandNum.genDTRandNum());
//        tGSAfficheBean.setGs_affiche_recordid(tOperBean.getGs_oper_id());
        tGSAfficheBean.setGs_affiche_recordid(context.getLoginUserId());
        tGSAfficheBean.setGs_affiche_time(buftime);
        if (tGSAfficheBean.getGs_affiche_content()== null || 
                tGSAfficheBean.getGs_affiche_content().equals("")){
            tGSAfficheBean.setGs_affiche_content(" ");
        }      
        try{
            //文件上传并保存该文件
            FormFile tFile = tForm.getFilename();      
            if(!(tFile.getFileSize() == 0)){
            	String upLoadPath = GSSystemConfigurator.getParam("gs.upload.savepath");
            	String tPath = "";
            	if(upLoadPath.startsWith(":/", 1)){
            		tPath = GSSystemConfigurator.getParam("gs.upload.savepath");
            	}else{
            		tPath=tForm.getSession().getServletContext().getRealPath(upLoadPath);
            	}	
            	if(tPath.equals("")||tPath == null){
            		tPath = GSSystemConfigurator.getParam("gs.upload.savepath");
            	}
                String tSaveName = tFile.getFileName();
                //文件类型
                String tFileType=tSaveName.substring(tSaveName.lastIndexOf("."));
                if(tFileType.equals(".xls")||tFileType.equals(".doc")||tFileType.equals(".xlsx")||tFileType.equals(".docx"))
                {
	                //文件系统时间
	                String buf = Long.toString(System.currentTimeMillis()/100); 
	                String tSaveFileName=tSaveName.substring(0,tSaveName.lastIndexOf("."));              
	                String tFileName=tSaveFileName+buf+tFileType;
	                GSAfficheProcessor.saveFileByRealPath(tFile,tPath,tFileName);
	                tGSAfficheBean.setGs_affiche_filename(tFileName);
                }else{
                	String tReturnMsg= "请选择正确的附件类型（xls,doc,xlsx,docx）";
                	aForm.getRequest().setAttribute("exception_code",GSException.E17793);
        	    	aForm.getRequest().setAttribute("exception_desc",tReturnMsg);
        	    	aForm.getRequest().setAttribute("window","true");
        	    	return GSBaseConstants.FORWARD_FAILURE;
                }
            }    
            GSAfficheProcessor.addTask(tGSAfficheBean);
        }catch(GSException err){
            GSLogWriter.error(err.getMessage());
            err.printStackTrace();            
            aForm.getRequest().setAttribute("exception_desc",err.getMessage());
            aForm.getRequest().setAttribute("exception_code",err.getCode());
            aForm.getRequest().setAttribute("window","true");
            
            throw err;
        }
        
        aForm.getRequest().setAttribute("message", "新增公告信息成功!");
        aForm.getRequest().setAttribute("backstep", "0");
        aForm.getRequest().setAttribute("requery","true");
        
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
